<?php
require_once "koneksi.php";
require_once "lib.php";
ini_set('max_execution_time', 3600);
$no = 0;
$data_siswa=get_data_siswa();
?>
<style>
	.alert {
		padding: 15px;
		margin-bottom: 20px;
		border: 1px solid transparent;
		border-radius: 4px;
		width: 300px;
		margin-left: auto;
		margin-right: auto;
		margin-top: 100px;
		font-family: 'Arial';
		font-size: 14px;
		text-align: center;
	}
	
	.alert-info {
		color: #31708f;
		background-color: #d9edf7;
		border-color: #bce8f1;
	}

</style>
<div class='alert alert-info'>Proses Pengiriman SMS</div>
<?php
foreach($data_siswa as $key=>$val){
	$tapel_aktif=get_tapel_aktif();
	$periode_aktif=get_periode_aktif();
	//print_r($periode_aktif);
	$tagihan_bulanan=get_komp_bulanan($tapel_aktif['id_mr_th_ajaran'],$val['nis'],$periode_aktif['id']);
	$tagihan_sekali=get_tagihan_sekali($val['id_tapel_masuk'],$val['nis']);
	$tagihan_tahunan=get_tagihan_tahunan($tapel_aktif['id_mr_th_ajaran'],$val['nis']);
	//echo "<pre>";
	//print_r($tagihan_bulanan);
	$arr_sms=array();
	//$str_sms="SAF : Tagihan ".$val['nm_lengkap']." ";
	//echo "SAF : Tagihan ".$val['nm_lengkap']." "."1 <br>";
	//$str_sms.=" Periode ".konvert_periode($periode_aktif['periode'])." ";
	$str_bulanan="Bulanan : ";
	$jml_bulanan=count($tagihan_bulanan);
	$no_bulanan=1;
	$tot_bulanan=0;
	foreach($tagihan_bulanan as $val_bulanan){
		/*
		if($no_bulanan==$jml_bulanan){
			$str_bulanan.=$val_bulanan['nama_komp']." : Rp. ".number_format($val_bulanan['nominal'],0,",",".")." ";
		}else{
			$str_bulanan.=$val_bulanan['nama_komp']." : Rp. ".number_format($val_bulanan['nominal'],0,",",".").", ";
		}
		*/
		$tot_bulanan+=$val_bulanan['nominal'];
		//$no_bulanan++;
	}
	$str_bulanan.=number_format($tot_bulanan,0,",",".");
	$arr_sms[]=array('komponen'=>'Bulanan','nominal'=>$tot_bulanan);
	//untuk tahunan
	$str_tahunan="Tahunan : ";
	$jml_tahunan=count($tagihan_tahunan);
	$no_tahunan=1;
	$tot_tahunan=0;
	foreach($tagihan_tahunan as $val_tahunan){
		/*
		if($no_tahunan==$jml_tahunan){
			$str_tahunan.=$val_tahunan['nama_komp']." : Rp. ".number_format($val_tahunan['nominal'],0,",",".")." ";
		}else{
			$str_tahunan.=$val_tahunan['nama_komp']." : Rp. ".number_format($val_tahunan['nominal'],0,",",".").", ";
		}
		*/
		$tot_tahunan+=$val_tahunan['nominal'];
		$no_tahunan++;
	}
	$str_tahunan.=number_format($tot_tahunan,0,",",".");
	$arr_sms[]=array('komponen'=>'Tahunan','nominal'=>$tot_tahunan);
	//akhir tahunan
	//awal sekali
	$str_sekali="Sekali : ";
	$jml_sekali=count($tagihan_sekali);
	$no_sekali=1;
	$tot_sekali=0;
	foreach($tagihan_sekali as $val_sekali){
		$bayar=get_bayar_sekali($val['nis'],$val_sekali['id_komponen']);
		$jum_tagihan=$val_sekali['nominal'];
		$sisa=$jum_tagihan-$bayar;
		/*
		if($no_sekali==$jml_sekali){
			$str_sekali.=$val_sekali['nama_komp']." Kurang : Rp. ".number_format($sisa,0,",",".")." ";
		}else{
			$str_sekali.=$val_sekali['nama_komp']." Kurang : Rp. ".number_format($sisa,0,",",".").", ";
		}
		*/
		$tot_sekali+=$sisa;
		$no_sekali++;
	}
	$str_sekali.=number_format($tot_sekali,0,",",".");
	$arr_sms[]=array('komponen'=>'Sekali','nominal'=>$tot_sekali);
	//akhir sekali
	$str_tunggakan="Tunggakan : ";
	$tot_tunggakan=get_tunggakan_total($tapel_aktif['id_mr_th_ajaran'],$val['nis']);
	//$tot_tunggakan=get_tunggakan_total(10,$val['nis']);
	$str_tunggakan.=number_format($tot_tunggakan,0,",",".");
	$arr_sms[]=array('komponen'=>'Tunggakan','nominal'=>$tot_tunggakan);

	foreach($arr_sms as $key_sms=>$val_sms){
		if($val_sms['nominal']==0){
			unset($arr_sms[$key_sms]);
		}
	}
	$det_wali=get_detail_wali($val['nis']);
	if(!empty($det_wali)){
		$nohp_wali=$det_wali['tlp'];
	}else{
		$nohp_wali="";
	}
	$no++;
	//echo $no.' no wali '.$nohp_wali."(".$det_wali['hubungan'].")<br>";
	$str_sms="";
	if(count($arr_sms)>0){
		$no_sms=1;
		//$str_sms="SAF : ".substr($detail_siswa[0]->nm_lengkap,0,20)." telah melakukan pembayaran bulan ".$str_periode." ";
		$str_sms="Assalaamu'alaikum. SAF : Tagihan ".$val['nm_lengkap']." ";
		$str_sms.=" Periode ".konvert_periode($periode_aktif['periode'])." ";
		foreach($arr_sms as $val_sms){
			if(count($arr_sms)==$no_sms){
				$str_sms.=$val_sms['komponen']." : ".number_format($val_sms['nominal'],0,",",".");
			}else{
				$str_sms.=$val_sms['komponen']." : ".number_format($val_sms['nominal'],0,",",".").", ";
			}
			$no_sms++;
		}
		$str_sms .=". Trmksh";
		if($nohp_wali!=""){
			//echo $str_sms."<br>";
			kirim_sms($nohp_wali,$str_sms);
		}
		
	}
}
echo "<meta http-equiv='refresh' content='0; url=pesan.php?t=sms'>";
?>